#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//账号密码
typedef struct
{  
    char id[10];
    char code[10];
}account_password;

//录入的个人信息
typedef struct 
{
    int id;
    char name[50];
    char gender[10];
    char phone[15];
}information;

//定义一个结构体链表
typedef struct node
{
    information data;
    struct node*next;
}node;

node *head=NULL;

//链表初始化
/*node* initList()
{
    node*head = (node*)malloc(sizeof(node));
    head=NULL;
    head->next = NULL;//初始化链表
    return head;
}*/

//创建通讯录
void CreateNode()
{
    if(head != NULL)
    {
        printf("通讯录已存在\n");
        return;
    }
    head = NULL;
    //initList();
    printf("创建通讯录成功\n");
}

//链表的增加（头插法）
/*void insertHead(node*L,int a,const char*b,const char*c,const char*d)
{
    node*P = (node*)malloc(sizeof(node));
    P->data.id = a;
    strcpy(P->data.name,b);
    strcpy(P->data.gender,c);
    strcpy(P->data.phone,d);
    P->next = L->next;
    L->next = P;
    
}*/
void addinsertHead()
{
    node*P = (node*)malloc(sizeof(node));
    printf("=======添加联系人========\n");
    printf("请输入编号:");
    scanf("%d",&P->data.id);

    printf("请输入姓名:");
    scanf("%s",P->data.name);

    printf("请输入性别:");
    scanf("%s",P->data.gender);

    printf("请输入电话:");
    scanf("%s",P->data.phone);

    if(head!=NULL)
    {
        P->next = head->next;
        head->next = P;
    }
    else
    {
        head=P;
    }

    //P->next = head->next;
    //head->next = P;

    FILE *fp;
    fp=fopen("2.txt","a+");
    if(NULL==fp)
    {
        printf("联系人添加失败\n");
        return;
    }
    fprintf(fp, "%d,%s,%s,%s\n", 
        P->data.id, P->data.name, P->data.gender, P->data.phone);    
    fclose(fp);
    printf("联系人添加成功\n");
}

//链表的查找
/*void findList()
{
    if(head==NULL)
    {
        printf("通讯录为空\n");
        return;
    }
    char name[50];
    printf("请输入联系人:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';
    //scanf("%s",name);
    node*current=head;
    while(current !=NULL)
    {
        if(strcmp(current->data.name,name)==0)
        {
            printf("编号：%d\n",current->data.id);
            printf("姓名：%s\n",current->data.name);
            printf("性别：%s\n",current->data.gender);
            printf("电话：%s\n",current->data.phone);
            return;
        }
        current=current->next;
    }
    printf("未找到此姓名\n");
}*/
//链表的查找有返回值版
node*findList()
{
    if(head==NULL)
    {
        printf("通讯录为空\n");
        return NULL;
    }
    char name[50];
    printf("请输入联系人:");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';
    //scanf("%s",name);
    node*current=head;
    while(current !=NULL)
    {
        if(strcmp(current->data.name,name)==0)
        {
            printf("编号：%d\n",current->data.id);
            printf("姓名：%s\n",current->data.name);
            printf("性别：%s\n",current->data.gender);
            printf("电话：%s\n",current->data.phone);
            return current;
        }
        current=current->next;
    }
    printf("未找到此姓名\n");
    return NULL;
}

//保存链表到文件的函数
void saveToFile(const char* filename)
{
    FILE* fp = fopen(filename, "a");  // 用 "w" 覆盖原文件
    if (fp == NULL)
    {
        printf("无法打开文件 %s\n", filename);
        return;
    }

    node* current = head;
    while (current != NULL)
    {
        // 按格式写入文件，例如：id,name,gender,phone
        fprintf(fp, "%d,%s,%s,%s\n",
                current->data.id,
                current->data.name,
                current->data.gender,
                current->data.phone);
        current = current->next;
    }

    fclose(fp);
    printf("数据已保存到文件 %s\n", filename);
}

//链表的修改
void modifynode()
{
    //node*List=head;
    node*target=findList();
    if(target==NULL)
    {
        printf("无内容无法修改");
        return;
    }


    int modifymenu = -1;
    while(1)
    {
        printf("1、修改编号\n");
        printf("2、修改姓名\n");
        printf("3、修改性别\n");
        printf("4、修改电话\n");
        printf("5、全部修改\n");
        printf("6、返回\n");
        printf("请选择功能：");
        scanf("%d",&modifymenu);
        getchar();
        switch(modifymenu)
        {
            case 1:
            {
                int a;
                printf("请输入新编号：");
                scanf("%d",&a);
                target->data.id = a;
                saveToFile("2.txt");
                printf("编号修改成功\n");
                printf("%d\n",target->data.id);
                break;
            }
            case 2:
            {
                char arr[50];
                printf("请输入新姓名：");
                scanf("%s",arr);
                strcpy(target->data.name,arr);
                printf("姓名修改成功\n");
                printf("%s\n",target->data.name);
            }
            case 3:
            {
                char arr[10];
                printf("请输入新性别:");
                scanf("%s",arr);
                strcpy(target->data.gender,arr);
                printf("性别修改成功\n");
                printf("%s\n",target->data.gender);
            }
            case 4:
            {
                char arr[15];
                printf("请输入新电话：");
                scanf("%s",arr);
                strcpy(target->data.phone,arr);
                printf("电话修改成功");
                printf("%s\n",target->data.phone);
            }
            case 5:
            {
                int a;
                char arr[50];
                char brr[10];
                char crr[15];
                printf("请输入新编号：");
                scanf("%d",&a);
                target->data.id = a;

                printf("请输入新姓名：");
                scanf("%s",arr);
                strcpy(target->data.name,arr);

                printf("请输入新性别:");
                scanf("%s",brr);
                strcpy(target->data.gender,brr);
                
                printf("请输入新电话：");
                scanf("%s",crr);
                strcpy(target->data.phone,crr);
                printf("%d %s %s %s",
                    target->data.id,target->data.name,
                    target->data.gender,target->data.phone);
                printf("全部修改成功");
            }
            case 6:
            {
                return;
            }
        }
    }    
}

//删除功能
void deletenode()
{ 
    if (head == NULL)
    {
        printf("通讯录为空，无法删除！\n");
        return;
    }
    char name[15];
    printf("请输入要删除的联系人姓名：");
    fgets(name, sizeof(name), stdin);
    name[strcspn(name, "\n")] = '\0';
    node*current = head;
    node*prev = NULL;
    while(current != NULL)
    {
        if(strcmp(current->data.name,name)==0)
        {
            break;
        }
        prev = current;
        current = current->next;        
    }
    if(current == NULL)
    {
        printf("未找到联系人");
        return; 
    }
    if(prev == NULL)
    {
        head = current->next;
    }
    else
    {
        prev->next = current->next;
    }
    printf("联系人%s已被删除\n",name);
} 

//插入信息
void insertmessage()
{
    node*newnode = (node*)malloc(sizeof(newnode));
    if(newnode == NULL)
    {
        printf("分配内存失败");
        return;
    }
    printf("请输入添加的联系人编号:");
    scanf("%d",&newnode->data.id);

    printf("请输入添加的联系人姓名:");
    scanf("%s",newnode->data.name);

    printf("请输入添加的联系人性别:");
    scanf("%s",newnode->data.gender);

    printf("请输入添加的联系人电话:");
    scanf("%s",newnode->data.phone);
    newnode->next = NULL;

    int post;
    int i=0;
    printf("请输入要插入的位置:");
    scanf("%d",&post);

    while(newnode != NULL&&i<post-1);
    {
        newnode = newnode->next;
        i++;
        if(newnode==NULL)
        {
            return;
        } 
    }
    node*new1node = (node*)malloc(sizeof(newnode));
    new1node->next = newnode->next;
    newnode->next = new1node;
    printf("添加成功");   
}

//展示信息(遍历)
void shownode()
{
    node *L= head;
    int i=0;
    while(L!=NULL)
    {
        printf("%d\n",L->data.id);
        printf("%s\n",L->data.name);
        printf("%s\n",L->data.gender);
        printf("%s\n",L->data.phone);
        L = L->next;
        i++;
    }
    printf("%d\n",i);
}

//导出
void export()
{
    node*P=head;
    FILE *fp;
    fp=fopen("2.txt","w");
    if(NULL==fp)
    {
        printf("导出失败\n");
        return;
    }
    while(P!=NULL)
    {
        fprintf(fp, "%d,%s,%s,%s\n", 
        P->data.id, P->data.name, P->data.gender, P->data.phone); 
        P=P->next;   
    }
    fclose(fp);
}

//排序
void rank()
{
    node*i,*j;
    information temp;
    for(i=head;i->next!=NULL;i=i->next)
    {
        for(j=head;j->next!=NULL;j=j->next)
        {
            if(strcmp(j->data.name,j->next->data.name)>0)
            {
                temp=j->data;
                j->data=j->next->data;
                j->next->data=temp;
            }
        }
    }
    printf("排序完成\n");

}

//链表初始化
void initList()
{
    head=NULL;
    printf("链表已经初始化\n");
}

void getLength()
{
    int count = 0;
    node*current = head;
    while(current!=NULL)
    {
        count++;
        current = current->next;
    }
    printf("%d\n",count);
}

void counts()
{
    char arr[30]="man";
    int a=0;
    int b=0;
    node*current = head;
    while(current!=NULL)
    {
        if(strcmp(current->data.gender,arr)==0)
        {
            a++;
        }
        else
        {
            b++;
        }
        current = current->next;
    }
    printf("男生人数为%d人\n",a);
    printf("女生人数为%d人\n",b);
}

int main()
{
    int menu = -1;
    account_password person;
    while(1)
    {
        printf("=========通讯录==========\n");
        printf("=========1、注册==========\n");
        printf("=========2、登录==========\n");
        printf("=========3、退出==========\n");
        printf("请选择功能：");
        scanf("%d",&menu);
        getchar();

        switch(menu)
        {
            case 1:
            {
                printf("请输入个人信息：\n");

                printf("账号:");
                scanf("%s",person.id);
                getchar();

                printf("密码：");
                scanf("%s",person.code);

                FILE *fp;
                fp=fopen("/home/ubuntu/git/smart_home/9.23/1.txt","a+");
                if(NULL==fp)
                {
                    printf("注册失败\n");
                    return -1;
                }
                fprintf(fp," %s", person.id);
                fprintf(fp," %s\n",person.code);
                fclose(fp);
                printf("注册成功\n");
                break;
            }

            case 2:
            {
                account_password person1;
                account_password file;
                int found = 0;
                printf("请输入账号：");
                scanf("%s",person1.id);

                printf("请输入密码：");
                scanf("%s",person1.code);

                FILE *fp = fopen("/home/ubuntu/git/smart_home/9.23/1.txt", "r");
                if(fp == NULL)
                {
                    printf("无法打开密码文件\n");
                    return -1;   
                }
                while(fscanf(fp, "%s %s", file.id, file.code) != EOF)
                {
                    if(strcmp(file.id,person1.id)==0&&strcmp(file.code,person1.code)==0)
                    {
                        found = 1;
                        break;
                        //printf("登录成功\n");
                    }
                }  
                    fclose(fp); 
                    if(found == 1)
                    {
                        printf("登录成功\n");
                        int menu2 = -1;
                        while(1)
                        {
                            printf("=========1、创建模块creat==========\n");
                            printf("=========2、添加数据add============\n");
                            printf("=========3、查找信息find===========\n");
                            printf("=========4、修改信息update=========\n");
                            printf("=========5、删除信息delete=========\n");
                            printf("=========6、插入信息insert=========\n");
                            printf("=========7、展示信息show===========\n");
                            printf("=========8、导出信息export=========\n");
                            printf("=========9、按照姓名排序===========\n");
                            printf("=========10、返回上一级============\n");
                            printf("=========11、链表初始化============\n");
                            printf("=========12、获取链表长度==========\n");
                            printf("=========13、统计性别==============\n");
                            printf("请输入功能：");
                            scanf("%d",&menu2);
                            getchar();

                            switch(menu2)
                            {
                                case 1:
                                {
                                    CreateNode();
                                    break;
                                }
                                case 2:
                                {
                                    addinsertHead();
                                    break;
                                }
                                case 3:
                                {
                                    findList();
                                    break;
                                }
                                case 4:
                                {
                                    modifynode();
                                    break;
                                }
                                case 5:
                                {
                                    deletenode();
                                    break;
                                }
                                case 6:
                                {
                                    insertmessage();
                                    break;
                                }
                                case 7:
                                {
                                    shownode();
                                    break;
                                }
                                case 8:
                                {
                                    export();
                                    break;
                                }
                                case 9:
                                {
                                    rank();
                                    break;
                                }
                                case 10:
                                {
                                    printf("返回主菜单\n");
                                    break;
                                }
                                case 11:
                                {
                                    initList();
                                    break;
                                }
                                case 12:
                                {
                                    getLength();
                                    break;
                                }
                                case 13:
                                {
                                    counts();
                                    break;
                                }
                            }
                            if(menu2==10)
                            {
                                break;
                            }
                           
                        }
                    }
                    else
                    {
                        printf("登录失败\n");
                    }
            }
            break;
            case 3:
            {
                exit(0);
                break;
            }       
        }
    }
    return 0;
}